﻿using com.iot.core.boot;
using com.iot.core.exception;
using com.iot.core.infrastructure;

namespace com.iot.ht.Controllers.htsoft.service
{
    public class AppCheckHttp : AppCheck
    {
        private   IHttpClientFactory _httpClientFactory;
        HttpClient httpClient = null;         

        public override async Task<MyApiResult> Check()
        {
            var r=new MyApiResult();    
            try
            { 
                if (_httpClientFactory == null)
                {
                    _httpClientFactory = EngineContext.Current.Resolve<IHttpClientFactory>();
                }
                if (httpClient == null)
                {
                    httpClient = _httpClientFactory.CreateClient();
                    httpClient.Timeout = new TimeSpan(0, 0, 3);
                } 
                
                var response = await httpClient.GetAsync(HtSoftApp.CkArg1);
                r.Msg=await response.Content.ReadAsStringAsync();                 
            }
            catch(Exception ex)
            {
                r.Code = 1;
                r.Msg=MyExceptionUtil.GetExMsg(ex);
                httpClient.Dispose();
                httpClient = null;
            }
            return r;
        }
    }
}
